package com.zhoufy.tomcat.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created with IDEA by ChouFy on 2020/1/19.
 *
 * @author Zhoufy
 */
public class ClassLoadUtil extends ClassLoader {

    private String classLoaderName;
    private final String fileExtension = ".class";

    private String path;


    public void setPath(String path) {
        this.path = path;
    }

    public ClassLoadUtil(String classLoaderName) {
        this.classLoaderName = classLoaderName;
    }

    public ClassLoadUtil(ClassLoader parent, String classLoaderName) {
        super(parent);
        this.classLoaderName = classLoaderName;
    }

    @Override
    public String toString() {
        return "[classLoaderName='" + classLoaderName + ']';
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] data = this.loadClassData(name);
        return this.defineClass(name, data, 0, data.length);
    }

    private byte[] loadClassData(String name) {
        InputStream is = null;
        byte[] data = null;
        ByteArrayOutputStream baos = null;

        try {
            name = name.replace(".", "/");

            is = new FileInputStream(new File(this.path + name + this.fileExtension));
            baos = new ByteArrayOutputStream();
            int ch;
            while (-1 != (ch = is.read())) {
                baos.write(ch);
            }
            data = baos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if (baos != null) {
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return data;
    }


    public static void main(String[] args) throws Exception {

        ClassLoadUtil classLoadUtil = new ClassLoadUtil("aa");

        classLoadUtil.setPath("/Users/zhoufy/Desktop/");
        Class<?> aClass = classLoadUtil.loadClass("com.ImageHelper");


        // 获取系统类加载器
        ClassLoader loader = ClassLoader.getSystemClassLoader();
        System.out.println(loader);
        // sun.misc.Launcher$AppClassLoader@18b4aac2

        // 获取其上层：扩展类加载器
        ClassLoader extClassLoader = loader.getParent();
        System.out.println(extClassLoader);
        // sun.misc.Launcher$ExtClassLoader@60e53b93

        // 获取其上层：根类加载器 返回为null
        ClassLoader bootStrapClassLoader = extClassLoader.getParent();
        System.out.println(bootStrapClassLoader);
        // null


        //  用户自定义类加载器, 默认使用系统类加载器
        ClassLoader classLoader = ClassLoadUtil.class.getClassLoader();
        System.out.println(classLoader);
        // sun.misc.Launcher$AppClassLoader@18b4aac2

        // sting类使用引导类加载器（根类加载器）进行加载的  ---> java的核心类库都是使用引导类加载器进行加载的
        ClassLoader classLoader1 = String.class.getClassLoader();
        System.out.println(classLoader1);
        // null
    }
}
